package com.vietnam.power.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.vietnam.power.constant.AppConfigurationConstant;
import com.vietnam.power.constant.GeneralConstant;
import com.vietnam.power.dao.MessageDao;
import com.vietnam.power.domain.MessageSearchCriteria;
import com.vietnam.power.persitence.EmailNotification;
import com.vietnam.power.persitence.MessageInfo;
import com.vietnam.power.persitence.MessageTypeDictionary;
import com.vietnam.power.util.DateTimeUtil;

@Repository
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
public class MessageDaoImpl extends GenericDaoImpl implements MessageDao {

	public MessageInfo getMessageById(int id) {
		return findById(MessageInfo.class, id);
	}

	public void createMessage(MessageInfo mi) {
		save(mi);
	}

	@SuppressWarnings("unchecked")
	public List<MessageInfo> searchMessages(MessageSearchCriteria tsc) {
		Criteria crt = getSessionFactory().getCurrentSession().createCriteria(MessageInfo.class);

		if (StringUtils.isNotBlank(tsc.getKeyword())) {
			crt.add(Restrictions.ilike("subject", tsc.getKeyword()));
		}

		if (StringUtils.isNotBlank(tsc.getSection())) {
			crt.createAlias("messageTypeDictionary", "messageTypeDictionary");
			crt.add(Restrictions.eq("messageTypeDictionary.name", tsc.getSection()));
		}

		if (tsc.getStatus() != null) {
			crt.add(Restrictions.eq("status", tsc.getStatus()));
		}

		if (tsc.getEmail() != null) {
			crt.add(Restrictions.or(Restrictions.ilike("to", tsc.getEmail(), MatchMode.ANYWHERE),
					Restrictions.ilike("from", tsc.getEmail(), MatchMode.ANYWHERE)));
		}

		crt.addOrder(Order.desc("createdDate"));

		return crt.list();
	}

	@SuppressWarnings("unchecked")
	public List<EmailNotification> getScheduledMessages() {
		Criteria crt = getSessionFactory().getCurrentSession().createCriteria(EmailNotification.class);

		crt.add(Restrictions.eq("status", AppConfigurationConstant.MESSAGE.UNSENT));
		crt.add(Restrictions.lt("notifiedDate", DateTimeUtil.getCurrentDateByTimezone(GeneralConstant.VIET_NAM_TIMEZONE)));

		return crt.list();
	}

	public void updateMessage(MessageInfo mi) {
		merge(mi);
	}

	public void deleteMessage(MessageInfo mi) {
		delete(mi);
	}

	public MessageTypeDictionary getMessageTypeDictionary(String name) {
		Criteria crt = getSessionFactory().getCurrentSession().createCriteria(MessageTypeDictionary.class);

		crt.add(Restrictions.eq("name", name));
		return (MessageTypeDictionary) crt.uniqueResult();
	}

	public void updateEmailNotification(EmailNotification emailNotification) {
		save(emailNotification);
	}
}
